package com.itcast.service.factory;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

public class factoryDemo {

   public static final Map<String,String> ssmap =new HashMap<String, String>();
   public static final Map<String,Object> cxmap =new HashMap<String, Object>();

    static {
        ResourceBundle bean = ResourceBundle.getBundle("bean");
        Enumeration<String> keys = bean.getKeys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            String className = bean.getString(key);
            ssmap.put(key, className);
        }
    }

    public static Object method1(String key){
        Object bean = cxmap.get(key);
        if (bean==null){
            String className = ssmap.get(key);
            try {
                bean = Class.forName( className).newInstance();
                cxmap.put(key,bean);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return bean;
    }

}
